package com.example.hao.flowercityfm.HFAdapter.ViewHolder;

import android.content.Context;


import android.content.SharedPreferences;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.annotation.RequiresApi;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.example.hao.flowercityfm.Files.Cache;
import com.example.hao.flowercityfm.HFAdapter.Type3Adapter;
import com.example.hao.flowercityfm.R;
import com.example.hao.flowercityfm.application.MyApp;
import com.example.hao.flowercityfm.pojo.Type3ListItem;
import com.example.hao.flowercityfm.utils.HttpUtils;
import com.google.gson.Gson;


import java.io.IOException;

import butterknife.BindView;


public class Type3ViewHolder extends BaseTypeViewHolder {
    private final SharedPreferences fmData;
    private final Cache cache;
    private SharedPreferences.Editor edit;
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.rv)
    RecyclerView rv;
    private Type3Adapter type3Adapter;

    public static final String TAG = "Type3ViewHolder";

    public Type3ViewHolder(Context context, View itemView) {
        super(context, itemView);
        fmData = context.getSharedPreferences(Cache.FM_DATA, Context.MODE_PRIVATE);

        cache = new Cache(context);
        initView();
    }


    public void initView() {
        title.setText("今日推荐");
        rv.setLayoutManager(new GridLayoutManager(context, 3));
        type3Adapter = new Type3Adapter(context);
        rv.setAdapter(type3Adapter);
        rv.addItemDecoration(new ItemDecoration(context.getResources().getDimensionPixelOffset(R.dimen._5dp)));


        if (isNotCache()) {
            loadData();
        } else {
            String file = cache.readFile(FILE_NAME);
            Log.d(TAG, "initView: file  " + file);
            if (file == null) {
                loadData();
            } else {
                bindData(file);
            }

        }

    }

    private static final String URL = "https://api.myjson.com/bins/1ecduw";
    private static final String KEY = "Type3";
    private static final String VALUE = "999";
    private static final String DEF_VALUE = "null";
    private static final String FILE_NAME = "Type3Json.txt";

    private void loadData() {
        HttpUtils.method2(URL, new HttpUtils.OnOkCallBack() {
            @Override
            public void onFailure(@NonNull IOException e) {
                Log.e(MyApp.TAG, "onFailure: " + e.getMessage());
            }

            @RequiresApi(api = Build.VERSION_CODES.KITKAT)
            @Override
            public void onResponse(@NonNull String jsonStr) {
                edit = fmData.edit();
                edit.putString(KEY, VALUE);
                boolean commit = edit.commit();

                bindData(jsonStr);
                cache.writeFile(FILE_NAME, jsonStr);
            }
        });
    }

    private void bindData(String jsonStr) {
        Type3ListItem type3ListItem = new Gson().fromJson(jsonStr, Type3ListItem.class);
        type3Adapter.setItem(type3ListItem);
    }

    private boolean isNotCache() {
        return fmData.getString(KEY, DEF_VALUE).equals(DEF_VALUE);
    }
}
